<?php

/**
 * This class manages all HTTP requests concerning Status_flag.
 */
class StatusflagController extends Zend_Controller_Action {
    
    public function init()
    {
       $this->_helper->acl->allow('admin')
	    ->allow('user', array('index', 'list')); 
    }

    /**
     * Show all Status_flags.
     */
    public function indexAction() 
    {
	$this->listAction();
    }

    /**
     * Show all Status_flags.
     */
    public function listAction() 
    {
	$m = new Model_Statusflag;
	$this->view->status_flags = $m->listAll();
    }

    /**
     * Update data of an existing status_flag.
     * 
     * URL parameter id<int> is required.
     * 
     * Without POST data, a form is with the status_flags data is displayed.
     * With valid POST data, the status_flag is persisted and the status_flag is redirected
     * to /status_flag/list.
     * With invalid POST data, the form's values with error messages is displayed.
     */
    public function updateAction() 
    {
	$request = $this->getRequest();
	$id = (int)$request->getParam('id');
	$form = new Application_Form_Statusflag();
	$m = new Model_Statusflag;
	
	if ($request->isPost()) 
	{   
	    if ($form->isValid($request->getPost())) 
	    {
		$c = $form->getValues();
		$c['id'] = $id;
		
		// Additional validation
		$valid = true;
		if ($c['max_caresits'] < $c['max_children_under_age_1'])
		{
		    $valid = false;
		    $form->getElement('max_children_under_age_1')->addError('Gesamtzahl an Betreuungsplätzen überschritten!');	    
		}
		
		if($valid) {
		    $m->update($c);

		    return $this->_helper->redirector('list');
		}
	    }
	}
	else
	{
	    $c = $m->find($id);
	    
	    //Strip Minutes
	    $c['daily_caretime_from']=$this->strip_seconds($c['daily_caretime_from']);
	    $c['daily_caretime_to']=$this->strip_seconds($c['daily_caretime_to']);
	    
	    $form->populate($c);
	    
	}

	$this->view->form = $form;
    }
    
    function strip_seconds($time) {
	$split = explode(':', $time);
	return $split[0].':'.$split[1];
    }
}